package com.wut.clock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wut.clock.common.exception.Asserts;
import com.wut.clock.entity.Article;
import com.wut.clock.entity.Category;
import com.wut.clock.mapper.ArticleMapper;
import com.wut.clock.mapper.CategoryMapper;
import com.wut.clock.service.ICategoryService;
import com.wut.clock.vo.admin.CategoryVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yuwei
 * @Date: 2022/6/1 9:53
 * @Description: 标签和专栏
 */

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl implements ICategoryService {

    private final CategoryMapper categoryMapper;

    private final ArticleMapper articleMapper;


    @Override
    public List<CategoryVo> listCategories(Integer pageNum, Integer pageSize, String categoryName) {
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(categoryName)) {
            categoryWrapper.like(Category::getCategoryName, categoryName);
        }
        List<Category> categoryList = categoryMapper.selectList(categoryWrapper);
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            Integer articleCount = articleMapper.selectCount(new LambdaQueryWrapper<Article>().eq(Article::getCategoryId, category.getId()));
            categoryVo.setArticleCount(Objects.nonNull(articleCount) ? articleCount : new Integer(0));
            categoryVos.add(categoryVo);
        }
        return categoryVos;
    }

    @Override
    public void saveCategory(Category category) {
        //判断名称重复
        Category existCategory = categoryMapper.selectOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getCategoryName, category.getCategoryName()));
        if (Objects.nonNull(existCategory)) {
            Asserts.fail("添加失败，专栏名已存在");
        }
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(Category category) {
        //判断名称重复
        Category existCategory = categoryMapper.selectOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getCategoryName, category.getCategoryName())
                .ne(Category::getId,category.getId()));
        if (Objects.nonNull(existCategory)) {
            Asserts.fail("修改失败，该专栏名已存在");
        }
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteCategory(Integer categoryId) {
        //查询专栏下是否有文章
        Integer count = articleMapper.selectCount(new LambdaQueryWrapper<Article>().
                eq(Article::getCategoryId, categoryId));
        if (count > 0) {
            Asserts.fail("删除失败，该专栏下存在" + count + "篇文章");
        }
        categoryMapper.deleteById(categoryId);
    }

    @Override
    public List<Category> listCategoriesForSelect() {
        return categoryMapper.selectList(null);
    }
}
